#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>

int main()
{
    int fd = open("./bite", O_CREAT | O_RDWR | O_TRUNC, 0666);
    if(fd < 0)
    {
        perror("open fail:");
        return -1;
    }

    // 向bite.txt 文件写入
    const char* msg = "i like linux!";
    ssize_t write_cnt = write(fd, msg, strlen(msg));
    if(write_cnt < 0)
    {
        perror("write fail:");
        return -1;
    }

    // 将文件指针移动到开头
    if(lseek(fd, 0, SEEK_SET) == -1)
    {
        perror("lssek fail:");
        return -1;
    }

    // 读取文件内容并打印
    char buf[1024] = {0};
    int read_cnt = read(fd, buf, sizeof(buf));


    if(read_cnt < 0)
    {
        perror("read fail:");
        return -1;
    }
    buf[read_cnt] = 0;

    printf("%s\n", buf);
    

    close(fd);

    return 0;
}

